package com.example.yblin.hello24;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.util.Log;

import com.example.yblin.hello24.DataUtils.JsonUtil;
import com.example.yblin.hello24.DataUtils.LocalDataUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Map;

import static org.junit.Assert.*;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getTargetContext();

        assertEquals("com.example.yblin.hello24", appContext.getPackageName());
    }

    /*
    测试JsonUtil的analyze()方法
     */
    @Test
    public void analyze() {
//        JSONObject jsonObject;
//        try {
//            String JSON_SIMPLE = "{'wife':'gakki','age':22}";
//            jsonObject = new JSONObject(JSON_SIMPLE);
//            Map<String, String> map = JsonUtil.analyze(jsonObject);
//            //迭代输出
//            for (Map.Entry<String, String> entry:
//                    map.entrySet()) {
//                Log.i("analyze: ", entry.getKey() + " " + entry.getValue());
//            }
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }

        String JSON_SIMPLE = "{'wife':'gakki','age':22}";
        Map<String, String> map = JsonUtil.analyze(JSON_SIMPLE);
        for (Map.Entry<String, String> entry:
                map.entrySet()) {
            Log.i("analyze: ", entry.getKey() + " " + entry.getValue());
        }


    }





    @Test
    public void localDataTest() {
        Context context = InstrumentationRegistry.getTargetContext();
        LocalDataUtil.write(context, LocalDataUtil.DATABASE_USERDATA, LocalDataUtil.USERNAME, "新垣结衣");
        String s = LocalDataUtil.read(context, LocalDataUtil.DATABASE_USERDATA, LocalDataUtil.USERNAME);
        assertEquals("新垣结衣", s);
    }

}
